package com.math.util;

import java.math.BigDecimal;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;

import com.logic.service.AdminService;

public class GaussianNumberGenerator {

	@Autowired
	protected AdminService adminService;
	
	public static void main(String... aArgs) {
		GaussianNumberGenerator gaussian = new GaussianNumberGenerator();
		double MEAN = 0.8f;
		double VARIANCE = 0.2f;
		for (int idx = 1; idx <= 10000; ++idx) {
			//double value=gaussian.getRandomGaussianValue(MEAN, VARIANCE);
			double value=gaussian.getRandomGaussianValueRange(MEAN, VARIANCE,0.0,1.0);
			//if(value>1)
				log("Generated : "+ value);
		}
	}

	private Random fRandom = new Random();

	public double getRandomGaussianValue(double aMean, double aVariance) {
		double gaussian=aMean + fRandom.nextGaussian() * aVariance;
		gaussian=roundDouble(gaussian,3);
		return gaussian;
	}
	
	public double getRandomGaussianValueRangeRounded(double aMean, double aVariance,double minValue,double maxValue) {
		double gaussian=aMean + fRandom.nextGaussian() * aVariance;
		gaussian=roundDouble(gaussian,3);
		
		if(gaussian>maxValue)
			return maxValue;
		else if (gaussian<minValue)
			return minValue;
		else 
			return gaussian;
	}
	
	public static double roundDouble(double value,int scale){
		BigDecimal val = new BigDecimal(value + "");
	    return val.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
	
	public double getRandomGaussianValueRange(double aMean, double aVariance,double minValue,double maxValue) {		
		double gaussian=0.0;
		do {
			gaussian=aMean + fRandom.nextGaussian() * aVariance;
			gaussian=roundDouble(gaussian,3);
		}
		while(gaussian<minValue || gaussian > maxValue);
		return gaussian;
	}

	private static void log(Object aMsg) {
		System.out.println(String.valueOf(aMsg));
	}

}
